<template>
	<view>
		<hx-navbar ref="hxnb" :config="config">
			<block slot="max">
				<view class="nav_wrap flex">
					<!-- <view class="back_wrap flex" @click="back">
						<u-icon name="arrow-left" color="#333" size="36"></u-icon>
					</view> -->
					<view class="page_title">消息</view>
				</view>
			</block>
		</hx-navbar>

		<view class="main-div">
			<image class="main-bg" v-if="meIndex==1" src="../../../static/img/lawyerVersion/me-bg1.png"
				mode="aspectFill"></image>
			<image class="main-bg" v-if="meIndex==2" src="../../../static/img/lawyerVersion/me-bg2.png"
				mode="aspectFill"></image>
			<view class="zhanwei" :style="{'height':statusBarHeight+'rpx'}"></view>
			<view class="m-d">
				<view class="tabdiv flex flex_sb">
					<view class="" @click="meIndex=1">
						<image class="me-icon" src="../../../static/img/lawyerVersion/me-icon1.png" mode=""></image>
						<view class="u-m-t-26">
							系统通知
						</view>
					</view>
					<view class="" @click="meIndex=2">
						<image class="me-icon" src="../../../static/img/lawyerVersion/me-icon2.png" mode=""></image>
						<view class="u-m-t-26">
							互动消息
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="xx-div" v-if="meIndex==1">
			<view class="xx-item" v-for="(item,index) in datalist" :key="index" @click="todetail(item)">
				<view class="flex flex_sb">
					<view class="line_1 xx-title">
						{{item.title}}
					</view>
					<view class="font_12_666">
						{{item.created_at}}
					</view>
				</view>
				<view class="font_14_666 u-m-t-14 xx-cen">
					{{item.title}}
				</view>
				<!-- <view class="flex flex_end xx-btn u-m-t-14">
					去认证 >
				</view> -->
			</view>
		</view>
		<view class="xx-div" v-if="meIndex==2">
			<!-- <view class="xx-item2 flex" v-for="(item,index) in datalist2" :key="index"  @click="showmo(item)">
				<view class="headimg">
					<u-badge v-if="item.is_read!=1" is-dot type="error" :offset="[0,0]"></u-badge>
					<image class="headimg" :src="item.image" mode="aspectFill"></image>
				</view>
				
				<view class="u-m-l-20">
					<view class="flex flex_sb">
						<view class="font_weight">
							{{item.name}}
						</view>
						<view class="font_12_999">
							{{item.time_trans}}
						</view>
					</view>
					<view class="font_12_999 line_1 u-m-t-12 xx-cen2">
						{{item.content}}
					</view>
				</view>
			</view> -->
			<view class="xx-item2 flex" v-for="(item,index) in conversationData" :key="index" @click="toMessage(item)">
				<view class="headimg">
					<u-badge v-if="item.unreadCount!=0" is-dot type="error" :offset="[0,0]"></u-badge>
					<!-- <view class="b7" v-if="item.unreadCount!=0">{{item.unreadCount}}</view> -->
					<image :src="item.userProfile.avatar" class="headimg"></image>
				</view>
				<view class="u-m-l-20">
					<view class=" font_weight">{{item.userProfile.nick}}</view>
					<view class="font_12_999  line_1 u-m-t-12 xx-cen2">{{item.lastMessage.messageForShow}}</view>
				</view>
			</view>
		</view>
		
		<u-modal title="消息" v-model="xxshow" :content="content"></u-modal>
				

		<u-tabbar active-color="#FA3B21" :list="tabbar"></u-tabbar>
		
		<serverTime></serverTime>
	</view>
</template>

<script>
	import TIM from '../../../../TXIM/static/IM-SDK/tim-wx.js'
	var statusBarHeight = (uni.getSystemInfoSync().statusBarHeight * 2);
	import serverTime from '../../../components/serverTime/serverTime.vue';
	export default {
		components: {
			serverTime
		},
		data() {
			return {
				statusBarHeight: statusBarHeight,
				config: {
					maxSlot: true,
					back: false,
					barPlaceholder: false,
					backgroundColor: [0, '#fff'],
					slideBackgroundColor: [1, '#fff']
				},
				tabbar: '',

				meIndex: 1,
				xxshow:false,
				datalist:[],
				datalist2:[],
				
				content:'',
				
				userID: '',
				userSign: '',
				conversationData: []
			}
		},
		onPageScroll(e) {
			// 重点，用到滑动切换必须加上
			this.$refs.hxnb.pageScroll(e);
		},
		onLoad() {
			this.tabbar = [{
				iconPath: "/static/img/tabBar/1-2.png",
				selectedIconPath: "/static/img/tabBar/1-1.png",
				text: '首页',
				pagePath: "/subPackageA/pages/lawyerVersion/index/index"
			}, {
				iconPath: "/static/img/tabBar/2-2-2.png",
				selectedIconPath: "/static/img/tabBar/2-2-1.png",
				text: '工作台',
				// count: 2,
				isDot: true,
				pagePath: "/subPackageA/pages/lawyerVersion/staging/staging"
			}, {
				iconPath: "/static/img/tabBar/4-1.png",
				selectedIconPath: "/static/img/tabBar/4-2.png",
				text: '消息',
				// count: 2,
				isDot: false,
				pagePath: "/subPackageA/pages/lawyerVersion/message/message"
			}, {
				iconPath: "/static/img/tabBar/5-2.png",
				selectedIconPath: "/static/img/tabBar/5-1.png",
				text: '我的',
				pagePath: "/subPackageA/pages/lawyerVersion/mine/mine"
			}]
			
			
			
			
		},
		onShow() {
			this.getList()
			
			this.getInfo()
			if (TIM.EVENT.SDK_READY == 'sdkStateReady') {
				this.getConversationList();
			}
		},
		methods: {
			//个人信息
			getInfo() {
				this.$api({
					url: '/getUserInfo',
					method: 'get',
					data: {
						token: uni.getStorageSync("token")
					},
				}).then(res => {
					if (res.code == 1) {
						this.$api({
							url: '/user/getImUserSign',
							method: 'post',
							data: {
								token: uni.getStorageSync("token"),
								user_id: res.data.id
							},
						}).then(res => {
							if (res.code == 1) {
								this.userSign = res.data.userSign
								this.userID = res.data.userId
								this.timLogin();
							}
						})
					}
				})
			},
			/* 登录 */
			timLogin() {
				var that = this;
				that.tim.login({
					userID: that.userID, //登录时的用户ID
					userSig: that.userSign
				}).then(function(imResponse) {
					console.log("登录结果", imResponse)
					//获取会话列表(必须等待TIM处于Ready状态才可获取，所以加了这么一行监听);
					that.tim.on(TIM.EVENT.SDK_READY, function(event) {
						that.getConversationList();
					}, that);
			
			
					uni.hideLoading();
				})
			},
			/* 获取会话列表 */
			getConversationList() {
				var that = this;
				let promise = that.tim.getConversationList();
				promise.then(function(imResponse) {
					const conversationList = imResponse.data.conversationList; // 全量的会话列表，用该列表覆盖原有的会话列表
					that.conversationData = conversationList;
					console.log("回话列表：", that.conversationData);
				});
			},
			/* 跳转消息页 */
			toMessage(item) {
				//垃圾组件不触发 只能嵌套触发的垃圾东西
				uni.$on('send', () => {
					uni.$emit("conversationData", item);
				})
				uni.navigateTo({
					url: '/TXIM/pages/chat/chat'
				})
			},
			showmo(item){
				this.xxshow=true
				this.content=item.content
			},
			todetail(e){
				uni.navigateTo({
					url:"/pages/mine/messageDetail?id="+e.id
				})
			},
			getList() {
				this.$api({
					url: '/getNoticeList',
					method: 'get',
					data: {
						token: uni.getStorageSync("token"),
						page:1,
						limit:1000,
						
					},
				}).then(res => {
					if (res.code == 1) {
						this.datalist = res.data.list
						
					}
				})
				
				// this.$api({
				// 	url: '/lawyer/msg_list',
				// 	method: 'get',
				// 	data: {
				// 		token: uni.getStorageSync("token"),
				// 		type:2, //1用户登入 2律师登入
				// 	}, 
				// }).then(res => {
				// 	if (res.code == 1) {
				// 		this.datalist2 = res.data.list
						
				// 	}
				// })
			},
		}
	}
</script>

<style scoped lang="scss">
	.xx-div {
		.xx-item {
			width: 686rpx;
			padding: 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 0 auto;
			margin-top: 20rpx;

			.xx-title {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				width: 362rpx;
			}

			.xx-cen {
				line-height: 40rpx;
			}

			.xx-btn {
				font-size: 28rpx;
				color: #5389ED;
			}
		}
		.xx-item2{
			width: 686rpx;
			padding: 30rpx 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 0 auto;
			margin-top: 20rpx;
			
			.headimg{
				width: 88rpx;
				height: 88rpx;
				border-radius: 50%;
				position: relative;
				background-color: #eee;
			}
			.xx-cen2{
				width: 528rpx;
			}
		}
	}

	.nav_wrap {
		width: 100%;
		position: relative;

		.back_wrap {
			position: absolute;
			left: 24rpx;

			.diqu {
				font-weight: 600;
				font-size: 32rpx;
				color: #FFFFFF;
				margin: 0 12rpx;
			}

			.dq-icon {
				width: 28rpx;
				height: 34rpx;
			}
		}

		.page_title {
			width: 100vw;
			text-align: center;
			font-weight: bold;
			font-size: 36rpx;
			color: #333;
		}
	}

	.main-div {
		position: relative;
		width: 100%;
		height: 358rpx;
		display: flex;
		flex-direction: column;

		.main-bg {
			width: 100%;
			height: 358rpx;
			position: absolute;
			top: 0;
			z-index: 0;
		}

		.zhanwei {
			width: 100%;
		}
	}

	.m-d {
		position: relative;
		padding-top: 88rpx;
		z-index: 2;
		padding-bottom: 40rpx;
		width: 100%;
		flex: 1;

		.tabdiv {
			position: absolute;
			width: 100%;
			bottom: 36rpx;
			padding: 0 160rpx;

			.me-icon {
				width: 88rpx;
				height: 88rpx;
				margin: 0 auto;
			}
		}
	}
</style>